package guestbook.server;

import guestbook.client.GreetingService;

import javax.mail.internet.MimeMessage;

import poppy.mail.SendMail;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;

/**
 * The server side implementation of the RPC service.
 */
@SuppressWarnings("serial")
public class GreetingServiceImpl extends RemoteServiceServlet implements
		GreetingService {

	public String greetServer(String input) {
		String serverInfo = getServletContext().getServerInfo();
		String userAgent = getThreadLocalRequest().getHeader("User-Agent");
		boolean result = sendTestMessage(input);
		return result ? "Message sent successfully" : "message delivery failed";
		//return XMLParser.fetchMeSomeXML("http://www.w3schools.com/xml/note.xml");
//		return "Hello, " + input + "!<br><br>I am running " + serverInfo
//				+ ".<br><br>It looks like you are using:<br>" + userAgent;
	}
	
    private boolean sendTestMessage(String recipient) {
    	MimeMessage message = SendMail.newMimeMessageToSubjBody(recipient, "Robots like to send email too!", "Hello, " + recipient + ".  Wazzup?");
        return SendMail.sendEmail(message);
    }
}
